package com.swgk.core.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;

import com.swgk.core.util.MLog;

import java.lang.ref.WeakReference;

public class DialogHelperNew {

    private static WeakReference<LoadingDialogNew> sProgressDialogRef;

    public static LoadingDialogNew showProgressDialog(Context context) {
        return showProgressDialog(context, null);
    }

    public static LoadingDialogNew showProgressDialog(Context context, String message) {
        return showProgressDialog(context, null, message, 0, true, null);
    }

    public static LoadingDialogNew showProgressDialog(Context context, String message, int imageId) {
        return showProgressDialog(context, null, message, imageId, true, null);
    }

    public static LoadingDialogNew showProgressDialog(Context context, String message, int imageId, boolean cancelable) {
        return showProgressDialog(context, null, message, imageId, cancelable, null);
    }

    public static void showProgressDialog2(Context context, String title, String message, int imageId,
                                           boolean canCancelable, DialogInterface.OnCancelListener listener, boolean canceledOnTouchOutside) {
        if (context != null && !((Activity) context).isFinishing()) {
            LoadingDialogNew loadingDialog = showProgressDialog(context, title, message, imageId, canCancelable, listener);
            loadingDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        }
    }

    @Deprecated
    public static LoadingDialogNew showProgressDialog(Context context, String title, String message, int imageId,
                                                      boolean canCancelable, DialogInterface.OnCancelListener listener) {
        LoadingDialogNew dialog = getDialog();
        if (dialog != null && dialog.getContext() != context) {
            // maybe existing dialog is running in a destroyed activity cotext we should recreate one
            dismissProgressDialog();
            MLog.e("dialog", "there is a leaked window here,orign context: " + dialog.getContext() + " now: " + context);
            dialog = null;
        }

        if (dialog == null) {
            dialog = new LoadingDialogNew(context, message, imageId);
            sProgressDialogRef = new WeakReference<>(dialog);
        }

        if (!TextUtils.isEmpty(title)) {
            dialog.setTitle(title);
        }
        if (!TextUtils.isEmpty(message)) {
            dialog.setMessage(message);
        }
        dialog.setCancelable(canCancelable);
        dialog.setOnCancelListener(listener);
        dialog.show();
        return dialog;
    }

    public static void dismissProgressDialog() {
        LoadingDialogNew dialog = getDialog();
        if (null == dialog) {
            return;
        }
        sProgressDialogRef.clear();
        if (dialog.isShowing()) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
                Log.e("异常",e.getMessage());
                // maybe we catch IllegalArgumentException here.
            }
        }

    }

    public static void setMessage(String message) {
        LoadingDialogNew dialog = getDialog();
        if (null != dialog && dialog.isShowing() && !TextUtils.isEmpty(message)) {
            dialog.setMessage(message);
        }
    }


    public static boolean isShowing() {
        LoadingDialogNew dialog = getDialog();
        return (dialog != null && dialog.isShowing());
    }

    private static LoadingDialogNew getDialog() {
        return sProgressDialogRef == null ? null : sProgressDialogRef.get();
    }
}
